<?php
class ControllerCatalogPage extends AdminController { 

	public function index() {
		M('catalog/page', 'page');		
		$this->getList();
	}

	public function insert() {
		M('catalog/page', 'page');
				
		if ($this->request->isPost() && $this->validateForm()) {
			$page_id = $this->page->addPage($this->request->post);
			//$this->_toHTML($page_id, $this->request->post['seo_url']);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/page'));
		}

		$this->getForm();
	}

	public function update() {
		
		M('catalog/page', 'page');
		
		if ($this->request->isPost() && $this->validateForm()) {
			
			//$page_info = $this->page->getPage($this->request->get['page_id']);
			
			//$filename = dirname(DIR_APPLICATION) . '/page/' . ($page_info['seo_url'] ? $page_info['seo_url'] : $page_info['page_id']) . '.html';			
			//if (file_exists($filename)) unlink($filename);
			
			$this->page->editPage($this->request->get['page_id'], $this->request->post);
			
			//$this->_toHTML($page_info['page_id'], $this->request->post['seo_url']);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/page'));
		}

		$this->getForm();
	}
 
	public function delete() {
		M('catalog/page', 'page');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $page_id) {
				//$page_info = $this->page->getPage($page_id);
				//$filename = dirname(DIR_APPLICATION) . '/page/' . ($page_info['seo_url'] ? $page_info['seo_url'] : $page_info['page_id']) . '.html';
				//if (file_exists($filename)) unlink($filename);				
				$this->page->deletePage($page_id);
			}
			
			$this->session->set_flashdata('success', $this->_['text_success']);

			$this->redirect(UA('catalog/page'));
		}

		$this->getList();
	}
	
	public function html() {
		M('catalog/page', 'page');
		$pages = $this->page->getPages();
		foreach ($pages as $page) {
			$this->_toHTML($page['page_id'], $page['seo_url']);
		}
		$this->session->set_flashdata('success', $this->_['text_html_success']);
		$this->redirect(UA('catalog/page'));
	}
	
	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		
		$qf = new Query_filter();
		
		$page_total = $this->page->getTotalPages();
		$this->data['pages'] = $this->page->getPages($qf);
 
		$this->data['pagination'] = $this->pagination($page_total)->render();

		$this->display('catalog/page_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		$page_id = G('page_id');
		
		if ($page_id) {
			$this->data['action'] = UA('catalog/page/update', 'page_id=' . $page_id);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->page->getPage($page_id);
				$this->vo->data['page_content'] = $this->page->getPageContents($page_id);
			}
		} else {
			$this->data['action'] = UA('catalog/page/insert');
		}
		
		$this->data['languages'] = C('cache_language_2');
		
		//store
		M('setting/store');
		
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		if (isset($this->request->post['page_store'])) {
			$this->data['page_store'] = $this->request->post['page_store'];
		} elseif ($page_id) {
			$this->data['page_store'] = $this->page->getPageStores($page_id);
		} else {
			$this->data['page_store'] = array(0);
		}		

		//layout
		M('design/layout');		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
		
		if (isset($this->request->post['page_layout'])) {
			$this->data['page_layout'] = $this->request->post['page_layout'];
		} elseif ($page_id) {
			$this->data['page_layout'] = $this->page->getPageLayouts($page_id);
		} else {
			$this->data['page_layout'] = array();
		}


		$this->display('catalog/page_form.tpl');
	}

	protected function validateForm() {
		$this->load->library('form_validation', true);
		foreach ($this->request->post['page_content'] as $language_id => $value) {
			$this->form_validation->set_rules("page_content[$language_id][title]", '', 'required|range_length[3,64]', $this->_['error_title']);
			$this->form_validation->set_rules("page_content[$language_id][content]", '', 'required|min_length[3]', $this->_['error_content']);
		}
		
		if (!$this->form_validation->run()) {
			$this->setMessage('error_warning', $this->_['error_warning']);
			return false;
		}
		
		return true;
	}

	protected function validateDelete() {

		M('setting/store');
		
		foreach ($this->request->post['selected'] as $page_id) {
			if (C('config_account_id') == $page_id) {
				$this->setMessage('error_warning', $this->_['error_account']);
				return false;
			}
			
			if (C('config_checkout_id') == $page_id) {
				$this->setMessage('error_warning', $this->_['error_checkout']);
				return false;
			}
			
			if (C('config_affiliate_id') == $page_id) {
				$this->setMessage('error_warning', $this->_['error_affiliate']);
				return false;
			}
						
			$store_total = $this->model_setting_store->getTotalStoresByInformationId($page_id);

			if ($store_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_store'], $store_total));
				return false;
			}
		}
		return true;
	}
	
	protected function _toHTML($page_id, $filename = '') {
		$url = U('page/index', "page_id=$page_id");
		if (empty($filename)) $filename = $page_id;
		copy($url, dirname(DIR_APPLICATION) . "/page/{$filename}.html");
	}
}
?>